package com.ymatou.iapi.usersafe.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.usersafe.parameter.VerifytSecurityQuestionBean;
import com.ymatou.iapi.usersafe.service.VerifytSecurityQuestionCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.JUserSafe;
//import com.ymttest.business.service.UserCallService;
import com.ymttest.database.model.UserSecurityQuestion;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 验证安全问题 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_VerifytSecurityQuestion {
	private static VerifytSecurityQuestionBean verifytsecurityquestionBean;
	private static VerifytSecurityQuestionCall verifytsecurityquestionCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("验证安全问题");
	}

	@Before
	public void caseUp() {
		verifytsecurityquestionBean = new VerifytSecurityQuestionBean();
		verifytsecurityquestionCall = new VerifytSecurityQuestionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(verifytsecurityquestionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_VerifytSecurityQuestion_001() {
		Logger.start(true, "验证安全问题成功成功");
		try {
			int userid = 3;
			List<UserSecurityQuestion> list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			if (list == null || list.size() == 0) {
				JUserSafe.setSecurityQuestion(userid);
				list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			}
			UserSecurityQuestion userSecurityQuestion = list.get(0);
			verifytsecurityquestionBean.setUserQuestionId(userSecurityQuestion.getUserQuestionId());
			verifytsecurityquestionBean.setUserId(userid);
			verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer());
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("true", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(200d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("验证通过", mResult.get("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_VerifytSecurityQuestion_002() {
		Logger.start(false, "缺少参数UserId,失败");
		try {
			verifytsecurityquestionBean.setUserQuestionId(1);
			verifytsecurityquestionBean.setQuestionAnswer("test");
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("false", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(100d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("非法", mResult.get("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_VerifytSecurityQuestion_003() {
		Logger.start(false, "缺少QuestionId,失败");
		try {
			int userid = 3;
			verifytsecurityquestionBean.setUserId(userid);
			// verifytsecurityquestionBean.setUserQuestionId(1);
			verifytsecurityquestionBean.setQuestionAnswer("test");
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("false", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(100d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("非法", mResult.get("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_VerifytSecurityQuestion_004() {
		Logger.start(false, "缺少QuestionAnswer,失败");
		try {
			int userid = 3;
			verifytsecurityquestionBean.setUserId(userid);
			verifytsecurityquestionBean.setUserQuestionId(1);
			// verifytsecurityquestionBean.setQuestionAnswer("test");
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("false", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(100d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("用户问题答案不能为空", mResult.get("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_VerifytSecurityQuestion_005() {
		Logger.start(false, "安全问题回答不正确,失败");
		try {
			int userid = TestDataManager.getRandomIntNum(5);
			UserSafeWapper usersecurity = new UserSafeWapper();
			List<UserSecurityQuestion> list = usersecurity.selectUsersecurityquestionByUserid(userid);
			if (list == null || list.size() == 0) {
				JUserSafe.setSecurityQuestion(userid);
				list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			}
			UserSecurityQuestion userSecurityQuestion = list.get(0);
			verifytsecurityquestionBean.setUserQuestionId(userSecurityQuestion.getUserQuestionId());
			verifytsecurityquestionBean.setUserId(userid);
			verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer() + 1);
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("false", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(100d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("用户的安全问题答案错误", mResult.get("Msg"), "验证Msg");
			int i = usersecurity.deleteVerifyLogByUserId(userid);
			Logger.comment("删除登入记录,条数:" + i);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_VerifytSecurityQuestion_006() {
		Logger.start(false, "UserQuestionId非法,失败");
		try {
			int userid = 3;
			// JUserSafe.setSecurityQuestion(userid);
			verifytsecurityquestionBean.setUserQuestionId(-1);
			verifytsecurityquestionBean.setUserId(userid);
			verifytsecurityquestionBean.setQuestionAnswer("测试1");
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("false", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(100d, mResult.get("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_VerifytSecurityQuestion_007() {
		Logger.start(false, "安全问题回答次数用完,失败");
		try {
			int userid = 8;
			List<UserSecurityQuestion> list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			if (list == null || list.size() == 0) {
				JUserSafe.setSecurityQuestion(userid);
				list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			}
			UserSecurityQuestion userSecurityQuestion = list.get(0);
			//每天超过连续2次答案错误后 返回 验证次数已用完
			for (int i = 0; i < 3; i++) {
				verifytsecurityquestionCall = new VerifytSecurityQuestionCall();
				verifytsecurityquestionBean.setUserQuestionId(userSecurityQuestion.getUserQuestionId());
				verifytsecurityquestionBean.setUserId(userid);
				verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer() + 1);
				verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
				verifytsecurityquestionCall.callService();
			}
			Logger.verifyEquals("false", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(0d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("验证次数已用完", mResult.get("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	//JIRACONN2522 现在不清0 只有在2天后验证安全问题后才清
	public void Tc_VerifytSecurityQuestion_008() {
		Logger.start(false, "安全问题回答次数用完后设备认证成功,次数清零");
		try {
			Logger.comment("前置条件准备");
//			String phone = TestDataManager.getPhoneNum();
//			String email = TestDataManager.getEmail();
//			String ip = TestDataManager.getRandomIp();
//			Map UserMap = UserCallService.registerUser(email, phone, ip);
//			String AccessToken = (String) UserMap.get("token");
//			int userid = (Integer) UserMap.get("userId");
//			String UserName = (String) UserMap.get("loginId");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userid = urrc.getUserid();
			List<UserSecurityQuestion> list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			if (list == null || list.size() == 0) {
				JUserSafe.setSecurityQuestion(userid);
				list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			}
			UserSecurityQuestion userSecurityQuestion = list.get(0);
			//每天超过连续2次答案错误后 返回 验证次数已用完
			for (int i = 0; i < 3; i++) {
				verifytsecurityquestionCall = new VerifytSecurityQuestionCall();
				verifytsecurityquestionBean.setUserQuestionId(userSecurityQuestion.getUserQuestionId());
				verifytsecurityquestionBean.setUserId(userid);
				verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer() + 1);
				verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
				verifytsecurityquestionCall.callService();
			}
			Logger.verifyEquals("false", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(0d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("验证次数已用完", mResult.get("Msg"), "验证Msg");
			
			
			List<Map<String, Object>> listold = new UserSafeWapper().getIPAuthByUserIdAndAreas(userid, null, null, null);
			String country=listold.get(0).get("Country").toString();
			String province=listold.get(0).get("Province").toString();
			String city=listold.get(0).get("City").toString();
			AuthCallServiceV2.authApp("clientid","deviceid",regbean.getLoginId(),JUserCallService.defaultLoginPwd, "App");
			
			verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer());
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("true", verifytsecurityquestionCall.getString("Success"), "验证Success");
			mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(200d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("验证通过", mResult.get("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	//JIRACONN2522 现在不清0 只有在2天后验证安全问题后才清
	public void Tc_VerifytSecurityQuestion_009() {
		Logger.start(false, "安全问题回答次数用完后ip认证成功,次数清零");
		try {
			int userid = 92;
			List<UserSecurityQuestion> list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			if (list == null || list.size() == 0) {
				JUserSafe.setSecurityQuestion(userid);
				list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			}
			UserSecurityQuestion userSecurityQuestion = list.get(0);
			//每天超过连续2次答案错误后 返回 验证次数已用完
			for (int i = 0; i < 3; i++) {
				verifytsecurityquestionCall = new VerifytSecurityQuestionCall();
				verifytsecurityquestionBean.setUserQuestionId(userSecurityQuestion.getUserQuestionId());
				verifytsecurityquestionBean.setUserId(userid);
				verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer() + 1);
				verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
				verifytsecurityquestionCall.callService();
			}
			Logger.verifyEquals("true", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(0d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("验证次数已用完", mResult.get("Msg"), "验证Msg");
			
			
			List<Map<String, Object>> listold = new UserSafeWapper().getIPAuthByUserIdAndAreas(userid, null, null, null);
			String country=listold.get(0).get("Country").toString();
			String province=listold.get(0).get("Province").toString();
			String city=listold.get(0).get("City").toString();
			AuthCallServiceV2.authIPArea("127.0.0.1","洋码头",JUserCallService.defaultLoginPwd, "App");
			
			verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer());
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("true", verifytsecurityquestionCall.getString("Success"), "验证Success");
			mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(200d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("验证通过", mResult.get("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_VerifytSecurityQuestion_010() {
		Logger.start(false, "安全问题回答次数用完后修改安全问题,次数清零JIRACONN2962");
		try {
			int userid = 100;
			List<UserSecurityQuestion> list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			if (list == null || list.size() == 0) {
				JUserSafe.setSecurityQuestion(userid);
				list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			}
			Logger.debug("安全问题列表："+FormatUtil.beanToGSONString(list));
			UserSecurityQuestion userSecurityQuestion = list.get(0);
			//每天超过连续2次答案错误后 返回 验证次数已用完
			for (int i = 0; i < 3; i++) {
				verifytsecurityquestionCall = new VerifytSecurityQuestionCall();
				verifytsecurityquestionBean.setUserQuestionId(userSecurityQuestion.getUserQuestionId());
				verifytsecurityquestionBean.setUserId(userid);
				verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer() + 1);
				verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
				verifytsecurityquestionCall.callService();
			}
			Logger.verifyEquals("false", verifytsecurityquestionCall.getString("Success"), "验证Success");
			Map mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(0d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("验证次数已用完", mResult.get("Msg"), "验证Msg");
			
			JUserSafe.setSecurityQuestion(userid);
			list = new UserSafeWapper().selectUsersecurityquestionByUserid(userid);
			userSecurityQuestion = list.get(0);
			verifytsecurityquestionBean.setUserQuestionId(userSecurityQuestion.getUserQuestionId());
			verifytsecurityquestionBean.setUserId(userid);
			verifytsecurityquestionBean.setQuestionAnswer(userSecurityQuestion.getQuestionAnswer());
			verifytsecurityquestionCall.setData(verifytsecurityquestionBean);
			verifytsecurityquestionCall.callService();
			Logger.verifyEquals("true", verifytsecurityquestionCall.getString("Success"), "验证Success");
			mResult = (Map) verifytsecurityquestionCall.getMap().get("Result");
			Logger.verifyEquals(200d, mResult.get("Code"), "验证Code");
			Logger.verifyIsContains("验证通过", mResult.get("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}